﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Text;
using OTS.Model;
using OTS.BLL;

public partial class Member_orders : OTS.MemberPage  //System.Web.UI.Page  OTS.MemberPage
{
    protected string strConent;
    custom cus = new custom();
    List<saleOrder> sOrderList = new List<saleOrder>();
    SaleOrderBLL sOderBLL = new SaleOrderBLL();
    DataTable dt = new DataTable();
    StringBuilder sb = new StringBuilder();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!TestLogin(HttpContext.Current))
        {
            Response.Redirect("index.aspx");
            return;
        }
        if (!Page.IsPostBack)
        {
            FillContent();
        }
    }

    private void FillContent()
    {
        string status = "";
        int yes = 0;
        int no = 0;
        this.lblSalesTotle.Text = "0";
        cus = GetCustom(HttpContext.Current);
        sOrderList = sOderBLL.GetListByCustomID(cus.cusID);

        if (sOrderList != null)
        {
            for (int i = 0; i < sOrderList.Count; i++)
            {
                sOrderList[i].list = sOderBLL.GetDetail(sOrderList[i].orderID);
                sb.Append("<tr id=\"trId" + sOrderList[i].orderID + "\">");
                sb.AppendFormat("<td>" + sOrderList[i].orderNO + "</td>");
                sb.Append("<td class=\"img-list\">");

                for (int j = 0; j < sOrderList[i].list.Count; j++)
                {
                    if (sOrderList[i].list.Count >= 2)
                    {
                        sb.Append("<div class=\"divstyle\">");
                        sb.AppendFormat("<a target=\"_blank\" href=\"product.aspx?proID=" + sOrderList[i].list[j].proID + "\">");
                        if (sOrderList[i].list[j].bigImg_pic=="")
                        {
                            sb.AppendFormat("<img src=\"img/yes_picture.gif\" alt=\"" + sOrderList[i].list[j].title + sOrderList[i].list[j].proNO + "\" />");
                        }
                        else
                        {
                            sb.AppendFormat("<img src=\"" + PictureProcessing.ImgPathToThumbURL(sOrderList[i].list[j].bigImg_pic) + "\" alt=\"" + sOrderList[i].list[j].title + sOrderList[i].list[j].proNO + "\" />");
                        }
                        sb.Append("</a>");
                        sb.Append("</div>");
                    }
                    else
                    {
                        sb.Append("<div>");
                        sb.AppendFormat("<a target=\"_blank\" href=\"product.aspx?proID=" + sOrderList[i].list[j].proID + "\">");
                        if (sOrderList[i].list[j].bigImg_pic == "")
                        {
                            sb.AppendFormat("<img src=\"img/yes_picture.gif\" alt=\"" + sOrderList[i].list[j].title + sOrderList[i].list[j].proNO + "\" />");
                        }
                        else
                        {
                            sb.AppendFormat("<img src=\"" + PictureProcessing.ImgPathToThumbURL(sOrderList[i].list[j].bigImg_pic) + "\" alt=\"" + sOrderList[i].list[j].title + sOrderList[i].list[j].proNO + "\" />");
                        }
                        sb.Append("</a>");
                        sb.Append("</div>");
                    }
                }
                sb.Append("</td>");
                sb.AppendFormat("<td>" + sOrderList[i].consignee + "</td>");
                sb.AppendFormat("<td>" + sOrderList[i].totalMoney + "</td>");
                sb.Append("<td>");
                sb.AppendFormat("<span style=\"color: #999;\">" + sOrderList[i].addTime + "</span>");
                sb.Append("</td>");
                sb.Append("<td style=\"background: none repeat scroll 0 0 #FFFFCC;\">");
                if (sOrderList[i].status == 0)
                {
                    status = "未付款";
                }
                else if (sOrderList[i].status == 1)
                {
                    status = "已付款";
                }
                else if (sOrderList[i].status == 2)
                {
                    status = "已发货/交易成功";
                }
                sb.AppendFormat("<strong style=\"color: #FF6600;\">" + status + "</strong>");
                sb.Append("</td>");
                sb.Append("<td>");
                if (sOrderList[i].status == 0)
                {
                    sb.Append("<div class=\"btn\">");
                    sb.Append("<a href=\"order-pay.aspx?orderId=" + sOrderList[i].orderID + "&orderNO=" + sOrderList[i].orderNO + "\">去付款</a>");
                    sb.Append("</div>");
                    sb.Append("<a id=\"doDelete" + sOrderList[i].orderID + "\" href=\"javascript:doDelete(" + sOrderList[i].orderID + ")\">删除</a>");
                }
                else if (sOrderList[i].status == 2)
                {
                    if (sOrderList[i].refund==0)
                    {
                        sb.Append("<a id=\"doDisplay" + sOrderList[i].orderID + "\" href=\"javascript:ordersDisplay(" + sOrderList[i].orderID + ")\">删除</a><br/><br/>");
                        sb.Append("<a id=\"oID" + sOrderList[i].orderID + "\" href=\"javascript:doRefund(" + sOrderList[i].orderID + ")\">申请退/换货</a>");
                    }
                    else if (sOrderList[i].refund==1)
                    {
                        sb.Append("<lable id=\"oID" + sOrderList[i].orderID + "\" style=\"color:#005EA7;\">退/换货审核中...</lable>");
                    }
                    else if (sOrderList[i].refund==2)
                    {
                        sb.Append("<lable id=\"oID" + sOrderList[i].orderID + "\" style=\"color:#005EA7;\">退/换货审核通过，退/换货中...</lable>");
                    }
                    else if (sOrderList[i].refund==3)
                    {
                        sb.Append("<lable id=\"oID" + sOrderList[i].orderID + "\" style=\"color:#005EA7;\">退/换货成功</lable>");
                    }
                }
                sb.Append("</td>");
                sb.Append("</tr>");

                if (sOrderList[i].status==1)
                {
                    yes += 1;
                }
                if (sOrderList[i].status==0)
                {
                    no += 1;
                }
                this.lblSalesTotle.Text = Convert.ToString(i + 1);
            }
            this.lblSalesY.Text = yes.ToString();
            this.lblSalesN.Text = no.ToString();
            strConent = sb.ToString();
        }
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        string id = this.txtId.Text;
        if (id!="0")
        {
            if (sOderBLL.Delete(id))
            {
                OTS.Common.Jscript.Alert("删除成功！");
                FillContent();
            }
            else
            {
                OTS.Common.Jscript.Alert("删除失败！");
                return;
            }
        }
    }
    protected void btnDisplay_Click(object sender, EventArgs e)
    {
        string id = this.txtId.Text;
        if (id != "0")
        {
            if (sOderBLL.Display(id))
            {
                OTS.Common.Jscript.Alert("删除成功！");
                FillContent();
            }
            else
            {
                OTS.Common.Jscript.Alert("删除失败！");
                return;
            }
        }
    }

    /// <summary>
    /// 申请退款
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmin_Click(object sender, EventArgs e)
    {
        string id = this.txtId.Text;
        if (this.txtRefund.Text.Trim()=="")
        {
            OTS.Common.Jscript.Alert("退款理由不能为空！");
            return;
        }
        if (id!="0")
        {
            if (sOderBLL.DoRefund(id,this.txtRefund.Text.Trim(),DateTime.Now.ToString()))
            {
                OTS.Common.Jscript.Alert("申请提交成功！");
                this.txtRefund.Text = "";
                FillContent();
            }
            else
            {
                OTS.Common.Jscript.Alert("申请提交失败！");
                return;
            }
        }
    }
}
